package com.franklin.icharm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Reminders;
import android.util.Log;

public class scReminder {
	private String reminderID;
	private String eventID;
	private String eventTitle;
	private Date eventStartDTime;
	private Date reminderTime;
	private boolean done = false;

	public boolean isDone() {
		return done;
	}

	public void setDone(boolean done) {
		this.done = done;
	}

	public String getReminderID() {
		return reminderID;
	}

	public void setReminderID(String reminderID) {
		this.reminderID = reminderID;
	}

	public String getEventID() {
		return eventID;
	}


	public void setEventID(String eventID) {
		this.eventID = eventID;
	}






	public String getEventTitle() {
		return eventTitle;
	}






	public void setEventTitle(String eventTitle) {
		this.eventTitle = eventTitle;
	}


	public Date getEventStartDTime() {
		return eventStartDTime;
	}






	public void setEventStartDTime(Date eventStartDTime) {
		this.eventStartDTime = eventStartDTime;
	}






	public Date getReminderTime() {
		return reminderTime;
	}






	public void setReminderTime(Date reminderTime) {
		this.reminderTime = reminderTime;
	}






	public scReminder() {
		// TODO Auto-generated constructor stub





	}
	public static ArrayList<scReminder> getReminders(Context context, String emailAccount)
	{
		return getReminders(context, emailAccount, true);
	}

	public static ArrayList<scReminder> getReminders(Context context, String emailAccount, boolean isToday)
	{
		final String[] EVENT_PROJECTION = new String[] {
				Calendars._ID,                           // 0
				Calendars.ACCOUNT_NAME,                  // 1
				Calendars.CALENDAR_DISPLAY_NAME,         // 2
				Calendars.OWNER_ACCOUNT                  // 3
		};
		final String TAG = "scReminders";
		ArrayList<scReminder> reminders = new ArrayList<scReminder>();
		Cursor cur = null;
		ContentResolver cr = context.getContentResolver();
		Uri uri = Calendars.CONTENT_URI;   
		String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND (" 
				+ Calendars.ACCOUNT_TYPE + " = ?) AND ("
				+ Calendars.OWNER_ACCOUNT + " = ?))";
		String[] selectionArgs = new String[] {emailAccount, "com.google",
				emailAccount}; 
		// Submit the query and get a Cursor object back. 
		cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);

		if ((cur != null) && cur.moveToFirst())
		{
			// Use the cursor to step through the returned records
			do{
				long calID = 0;
				String displayName = null;
				String accountName = null;
				String ownerName = null;

				// Get the field values
				calID = cur.getLong(0);
				displayName = cur.getString(1);
				accountName = cur.getString(2);
				ownerName = cur.getString(3);

				Log.d(TAG, String.format("%s %s %s %s", calID, displayName, accountName, ownerName ));

				String[] proj = 
						new String[]{
						Events._ID, 
						Events.TITLE,
						Events.DTSTART, 
						Events.DTEND, 
						Events.RRULE 
				};

				String selCal ;
				String[] selCalArg;
				if (isToday){
					selCal =  "((" + Events.CALENDAR_ID + " = ? ) AND  ("
							+ Events.DTSTART + " > ?) AND (" + Events.DTEND + "< ?))";

					long rightnow = (new Date()).getTime() + (60 * 1000); // plus one minutes

					selCalArg = new String[] {Long.toString(calID) , 
							String.valueOf(rightnow),
							String.valueOf(rightnow + (24*60*60*1000))
					};
				}else
				{
					selCal =  "(" + Events.CALENDAR_ID + " = ? )";
					selCalArg = new String[] {Long.toString(calID) };
				}
				Cursor cursor = 
						cr.query(
								Events.CONTENT_URI, 
								proj, 
								selCal, 
								selCalArg, 
								null);
				if (cursor != null && cursor.moveToFirst()) {
					do {

						String id_event = cursor.getString(0);
						String title_event= cursor.getString(1);
						String datetime_event = cursor.getString(2);
						Log.d(TAG, String.format("event_id:%s  title:%s start:%s", id_event, title_event, datetime_event));
						String[] proj_reminder = 
								new String[]{
								Reminders._ID,
								Reminders.MINUTES,
								Reminders.METHOD

						};
						Cursor cursor_reminder = 
								cr.query(
										Reminders.CONTENT_URI,
										proj_reminder, 
										Reminders.EVENT_ID + " = ? ", 
										new String[]{id_event}, 
										null);
						if (cursor_reminder != null && cursor_reminder.moveToFirst()) {
							do {

								String id_reminder = cursor_reminder.getString(0);
								String time_reminder= cursor_reminder.getString(1);
								String method_reminder = cursor_reminder.getString(2);
								scReminder scr = new scReminder();
								scr.setEventID(id_event);
								Date sdate = new Date();
								sdate.setTime(Long.parseLong(datetime_event));

								scr.setEventStartDTime(sdate);
								scr.setEventTitle(title_event);
								scr.setReminderID(id_reminder);
								Date sdate2 = new Date();

								sdate2.setTime(Long.parseLong(datetime_event) - (Long.parseLong(time_reminder) * 60 *1000));
								if (((new Date()).getTime() + (60 * 1000)) < sdate2.getTime())
								{
									scr.setReminderTime(sdate2);
									reminders.add(scr);
									Log.d(TAG, String.format("reminder_id:%s  mins:%s method:%s", id_reminder, time_reminder, method_reminder));

								}
							}while (cursor_reminder.moveToNext());
						}
					}while (cursor.moveToNext());
				}
			}while (cur.moveToNext()) ;
		}

		Collections.sort(reminders, new Comparator<scReminder>(){
			@Override
			public int compare(scReminder arg0, scReminder arg1) {
				return arg0.getReminderTime().compareTo(arg1.getReminderTime());
			}
		});

		return reminders;
	}

}
